<template>
  <a-card class="basicInfo-card" :bordered="false">
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline" @keyup.enter.native="searchQueryList">
        <a-row :gutter="10">
          <a-col :md="6" :sm="12" v-for="(item,index) in searchList" :key="index" style="margin-top: 5px">
            <j-tree-select
              ref="treeSelect"
              v-if="item.columnName==='area_code'"
              placeholder="请选择行政区划"
              v-model="queryParam[item.columnName]"
              dict="work_region,name,code"
              pid-field="parent_code"
              pidValue="37"
            >
            </j-tree-select>
            <a-input v-else v-model="queryParam[item.columnName]" :placeholder="'请输入'+item.columnDesc"/>
          </a-col>
          <span v-if="searchList.length > 0" style="float: left;overflow: hidden;margin-bottom: 10px;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px;margin-top: 5px;">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </a-col>
          </span>
        </a-row>
        <a-row>
          <a-col :md="6" :sm="24">
            <a-button style="margin-bottom: 10px" type="primary" @click="handleAddItem(1)">新增</a-button>
            <a-button
              @click="batchDel"
              v-if="selectedRowKeys.length > 0"
              ghost
              type="primary"
              icon="delete">批量删除
            </a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="small"
        bordered
        rowKey="id"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :pagination="ipagination"
        @change="handleTableChange">
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleAddItem(3, record)">详情</a>
          <a-divider type="vertical" />
          <a @click="handleAddItem(2, record)">编辑</a>
          <a-divider type="vertical" v-if="parseInt(record.isBind)!==1" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteData(record)" v-if="parseInt(record.isBind)!==1">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <resources-model ref="resourcesModel" @ok="getResourceList"></resources-model>
  </a-card>
</template>

<script>
import { postAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterObj } from '@/utils/util'
import ResourcesModel from './BaseResourcesModel'
export default {
  name: 'BaseResourcesList',
  components: {
    ResourcesModel
  },
  mixins: [JeecgListMixin],
  data() {
    return {
      width: 1200,
      departTree: [],
      orgTypeData: [],
      phoneWarning: '',
      searchList: [],
      dataSource: [],
      currSelected: [],
      columns: [],
      departName: '',
      title: '操作',
      gutter: 8,
      itemList: [],
      seen: false,
      visible: false,
      condition: true,
      layerDetail: {},
      disableSubmit: false,
      disableMixinCreated: true,
      showFields: [],
      model: {},
      threeCol: {
        label: { span: 6 },
        wrapper: { span: 24 - 6 }
      },
      markerLayerGroup: null,
      menuhidden: false,
      menuusing: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // 分页参数
      ipagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        add: '/subject/scSubjectResourceInfo/resourceDataAdd/',
        deleteData: '/subject/scSubjectResourceInfo/resourseDataDelete/',
        listUrl: '/subject/scSubjectResourceInfo/listResourceData/', // 图层资源数据列表
        deleteBatch: '/subject/scSubjectResourceInfo/resourceDataDeleteBatch/'
      }
    }
  },
  created() {
    // this.loadTree()
  },
  mounted() {
  },
  methods: {
    searchQueryList() {
      this.getResourceList(1)
    },
    // 删除图层数据
    handleDeleteData(record) {
      var that = this
      const key = record.id
      deleteAction(that.url.deleteData + this.currSelected.id, { id: key, layerId: this.currSelected.id }).then(resp => {
        if (resp.success) {
          that.$message.success('删除成功!')
          this.getResourceList(this.currSelected.id)
        } else {
          that.$message.warning('删除失败!')
        }
      })
    },
    batchDel: function() {
      var ids = ''
      for (var a = 0; a < this.selectedRowKeys.length; a++) {
        ids += this.selectedRowKeys[a] + ','
      }
      var that = this
      this.$confirm({
        title: '确认删除',
        content: '确定要删除所选中的数据吗?',
        onOk: function() {
          deleteAction(that.url.deleteBatch + that.currSelected.id, { ids: ids }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.getResourceList(1)
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    open(record, columns, searchList) {
      this.queryParam = {}
      this.columns = columns
      this.searchList = searchList
      this.currSelected = record
      console.log('currSelected:', this.currSelected)
      this.getResourceList(1, record.id)
    },
    handleTableChange(pagination, filters, sorter) {
      // 分页、排序、筛选变化时触发
      console.log('ddd:', pagination)
      // TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = sorter.order == 'ascend' ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.getResourceList()
    },
    handleAddItem(num, record) {
      if (num == 1) {
        console.log(this.currSelected)
        this.$refs.resourcesModel.add(this.currSelected)
        this.$refs.resourcesModel.title = '新增图层资源'
      } else if (num == 2) {
        this.$refs.resourcesModel.edit(record, this.currSelected)
        this.$refs.resourcesModel.title = '编辑图层资源'
        this.$refs.resourcesModel.disableSubmit = false
      } else {
        this.$refs.resourcesModel.edit(record, this.currSelected)
        this.$refs.resourcesModel.title = '图层资源详情'
        this.$refs.resourcesModel.disableSubmit = true
      }
    },
    getResourceList(arg, id) {
      if (!id) id = this.currSelected.id
      var that = this
      var listUrl = this.url.listUrl + id
      this.dataSource = []
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()// 查询条件
      console.log('params', params)
      postAction(listUrl, params).then(res => {
        if (res.success) {
          that.dataSource = res.result.data.records
          this.ipagination.total = res.result.data.total
        }
      })
    },
    getQueryParams() {
      const p = Object.assign({}, this.queryParam)
      const param = {}
      param.params = p
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    },
    searchReset() {
      this.queryParam = {}
      this.getResourceList(1)
    },
    searchQuery() {
      this.getResourceList(1)
    }
  }
}
</script>

<style scoped>

</style>
